﻿$(function () {
        $.ajax({
            url: '/Storeadmin/Staff/DepartmentLists',
            type: 'get',
            success: function (data) {
                $('.depLists').html(function () {
                    TreeList(0, data);
                    return menus.join('');
                });
                var depts = $('#DepartmentId').val().split(',');
                if (depts.length > 1) {
                    $.each(depts, function (index, item) {
                        $("input[type='checkbox'][value='" + item + "']").prop("checked", "checked");
                    });
                }
            }, error: function () {
                alert('系统出错');
            }
        });
    
    var menus = [];
    function TreeList(Id, arr) {
        var childarr = GetParentArray(Id, arr);
        if (childarr.length > 0) {
            menus.push('<ul>');
            for (var i in childarr) {
                menus.push('<li><input type="checkbox" value="'+childarr[i].Id+'">' + childarr[i].Name + '</li>');
                TreeList(childarr[i].Id,arr);
            }
            menus.push('</ul>');
        }
    }

    //获取主键下的其他菜单
    function GetParentArray(Id, arr) {
        var arry = [];
        for (var i in arr) {
            if (arr[i].Parentid == Id)
                arry.push(arr[i])
        }
        return arry;
    }
});